package com.heima.user.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.user.IUserClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.service.ApUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

//相当于把下面的controller接口注册到leadnews-feign-api里面，以提供给其它微服务调用
//其它微服务调用leadnews-feign-api里的服务的方法：
// 1.在其启动类里添加 @EnableFeignClients("com.heima.apis")
// 2.在Impl类里@Autowired IUserClient userClient;
// 3.调用userClient.findUserById(xxx);

@Slf4j
@RestController
public class UserClient implements IUserClient {

    @Autowired
    private ApUserService apUserService;

    /**
     * 根据用户ID查询app端的用户信息
     * @param id
     * @return
     */
    @Override
    @GetMapping("/api/v1/user/{id}")
    public ResponseResult findUserById(@PathVariable("id") Integer id) {
        log.info("远程调用了User微服务端的findUserById，参数：{}",id);
//        return apUserService.getById(id);

        ApUser apUser = apUserService.getOne
                (Wrappers.<ApUser>lambdaQuery().eq(ApUser::getId,id));
        //隐藏密码
        apUser.setSalt("***");
        apUser.setPassword("***");
        apUser.setPhone("***");
        return ResponseResult.okResult(apUser);
    }
}